//1.新建
let map=new Map()

//2.添加元素:map.set(key,value)
map.set("name","myName")
map.set("age",18)

//3.根据键取值：map.get(key)
console.log(map.get("name"));
console.log(map.get("name2"));//undefined

//4.是否存在键
console.log(map.has("name"));//true
console.log(map.has("name2")); //false
console.log("------------------------------------------------------");

//5.key迭代
console.log(map.keys());//[Map Iterator] { 'name', 'age' }
for(let key of map.keys()){
    console.log(key);
}
console.log("------------------------------------------------------");

//6.value迭代
console.log(map.values());//[Map Iterator] { 'myName', 18 }
for(let value of map.values()){
    console.log(value);
}
console.log("------------------------------------------------------");
//7.key,value迭代
console.log(map.entries());//[Map Entries] { [ 'name', 'myName' ], [ 'age', 18 ] }
for(let [key,value] of map.entries()){
    console.log(`${key}:${value}`);
}

//8.直接遍历
// 方法一：for...of
for (let [k, v] of map) {
    console.log(k, v);
}

// 方法二：forEach
map.forEach((value, key) => {
    console.log(key, value);
});
console.log("------------------------------------------------------");

//9.静态方法:部分node不支持
// const inventory = [
//     { name: "asparagus", type: "vegetables", quantity: 9 },
//     { name: "bananas", type: "fruit", quantity: 5 },
//     { name: "goat", type: "meat", quantity: 23 },
//     { name: "cherries", type: "fruit", quantity: 12 },
//     { name: "fish", type: "meat", quantity: 22 },
// ];
//
// const restock = { restock: true };
// const sufficient = { restock: false };
// const result = Map.groupBy(inventory, ({ quantity }) =>
//     quantity < 6 ? restock : sufficient,
// );
// console.log(result.get(restock));
// [{ name: "bananas", type: "fruit", quantity: 5 }]